//
//  PhoneUtil.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/22.
//

#import "PhoneUtil.h"
#import "TextUtil.h"

@implementation PhoneUtil

#pragma mark - 判断是否是手机号
+ (BOOL) isPhoneNumber:  (NSString *) phoneNumber {
    if ([TextUtil isEmpty:phoneNumber]) {
        return NO;
    }
    if (11 != phoneNumber.length) {
        return NO;
    }
    NSString *regex = @"^(1[3-9][0-9])\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:phoneNumber];
    return isMatch;
}

#pragma mark - 隐藏手机号
+ (NSString *) hideNumber: (NSString *) phoneNumber {
    if ([self isPhoneNumber:phoneNumber]) {
        NSMutableString * phoneStr = [NSMutableString stringWithString:phoneNumber];
            [phoneStr replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
            return phoneStr;
    }
    return phoneNumber;
}

#pragma mark - 跳转拨打电话界面
+ (void) go2Call: (NSString *) phoneNumber {
    NSString *telNumber = [NSString stringWithFormat:@"tel:%@", phoneNumber];
    NSURL *aURL = [NSURL URLWithString:telNumber];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:aURL]) {
        [app openURL:aURL options:nil completionHandler:^(BOOL success) {
            
        }];
    }
}

@end
